﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 根据不同的品质等级，加载不同AssetBundle资源
    /// </summary>
    [ExecuteInEditMode]
    public class QualityAssetBundleControl : MonoBehaviour
    {
        [Serializable]
        public struct ControlItem
        {
            /// <summary>
            /// 品质控制AssetBundle
            /// </summary>
            public AssetID Target;
            /// <summary>
            /// 控制对象在哪个品质等级显示
            /// </summary>
            public bool[] EnabledLevels;
        }

        [SerializeField]
        [Tooltip("The quality control items.")]
        private ControlItem[] controls;
        private LinkedListNode<Action> onQualityChangedNode; //品质切换回调事件
        private List<GameObject> objects;
        void Awake()
        {
            objects = new List<GameObject>();
            onQualityChangedNode = QualityConfig.ListenQualityChanged(OnQualityLevelChanged);
        }

#if UNITY_EDITOR
        private void Reset()
        {
            if (this.controls == null || this.controls.Length == 0)
            {
                var item = new ControlItem();
                item.Target = new AssetID();
                item.EnabledLevels = new bool[] { false, true, true };
                this.controls = new ControlItem[] { item };
            }
        }

        public void SetQualityLevel(int level)
        {
            ClearAllObjs();
            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }
                if (!ctrl.Target.IsEmpty && _enabled)
                {
                    var prefabPath = ctrl.Target.GetAssetPath();
                    var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
                    if (prefab == null)
                    {
                        prefab = UnityEditor.PrefabUtility.CreateEmptyPrefab(prefabPath);
                    }
                    GameObject x = (GameObject)UnityEngine.Object.Instantiate(prefab);
                    if (x == null)
                    {
                        Debug.LogError("CreateEffect | No resources: " + ctrl.Target.BundleName + " | " + ctrl.Target.AssetName);
                    }
                    else
                    {
                        x.transform.parent = this.transform;
                        x.transform.localPosition = Vector3.zero;
                        if (objects == null) objects = new List<GameObject>();
                        objects.Add(x);
                    }
                }
            }
        }



#endif
        private void OnDestroy()
        {
            if (onQualityChangedNode != null)
            {
                QualityConfig.UnlistenQualtiy(onQualityChangedNode);
                onQualityChangedNode = null;
            }
            ClearAllObjs();
            objects = null;
        }

        private void ClearAllObjs()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                if (objects != null)
                {
                    foreach (var go in objects)
                    {
                        if (go != null) go.SetActive(false);
                    }
                }
            }
            else
#endif
            if (objects != null)
                foreach (var go in objects)
                {
                    if (go != null) Destroy(go);
                }
        }

        private void OnQualityLevelChanged()
        {
            var level = QualityConfig.QualityLevel;
            ClearAllObjs();

            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }
                if (!ctrl.Target.IsEmpty && _enabled)
                {
                    LoadEffectAsync(ctrl.Target);
                }

            }
        }


        public void LoadEffectAsync(AssetID assetid)
        {
            AssetManager.LoadObject(assetid, typeof(GameObject));
        }
    }

}

